import Tools from '@/utils/Tools.js'
// #ifdef APP-PLUS
import permission from '@/js_sdk/AppPermission/permission.js'
// #endif
import { storageCache } from '@/store/dispatches.js';
const updateApplication = () => {
	// #ifdef MP
	const updateManager = uni.getUpdateManager();
	updateManager.onUpdateReady((res) => {
		uni.showModal({
			title: '更新提示',
			content: '新版本已经准备好，是否重启应用？',
			success: (res) => {
				if (res.confirm) {
					// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
					updateManager.applyUpdate();
				}
			}
		})
	})
	// #endif
	// #ifdef APP-PLUS

	// #endif
};

const getLocationPermission = async () => {
	// #ifdef APP-PLUS
	return permision.checkSystemEnableLocation()
	// #endif
	// #ifdef H5
	return await new Promise((resolve, reject) => {
		uni.getLocation({
			success: (res) => {
				if (res.errMsg == 'getLocation:ok') {
					resolve(true)
				} else {
					resolve(false)
				}
			},
			fail: () => {
				resolve(false)
			}
		})
	})
	// #endif
	// #ifdef MP
	return await new Promise((resolve, reject) => {
		uni.authorize({
			scope: 'scope.userLocation',
			success: () => {
				resolve(true)
			},
			fail: () => {
				resolve(false)
			}
		})
	})
	// #endif
}




//处理运行参数，:h5小程序在onLaunch中传入参数，APP在onShow中获取参数
const handleRuntimeArgs = (args) => {

}

/**
 * 绑定推送client_id
 */
const getClientId = () => {
	// #ifdef APP-PLUS
	console.log(plus.push.getClientInfo())
	return plus.push.getClientInfo().clientid;
	// #endif
	// #ifndef APP-PLUS
	return false;
	// #endif

}


// #ifdef APP-PLUS

/**
 * 获得分享功能提供服务商
 */
const cacheShareServiceProvider = () => {
	uni.getProvider({
		service: 'share',
		success: (res) => {
			storageCache({ key: 'shareServiceProvider', value: res.provider })
			console.log(res)

		}
	});
}

const initCacheSize = () => {
	plus.cache.calculate(size => {
		console.log(size + 'byte');
	});
}



// #endif


const analysisAppArguments = (str = '') => {
	let validity = false;
	let path = '';
	let param = {};
	if (str.indexOf('suisui://launch?') >= 0) {
		validity = true;
		path = decodeURIComponent(Tools.getUrlQuery(str, 'path') || '');
	}
	return { validity, path, param }
}




export default {
	updateApplication,
	handleRuntimeArgs,
	getLocationPermission,
	getClientId,
	// #ifdef APP-PLUS
	cacheShareServiceProvider,
	analysisAppArguments,
	initCacheSize
	// #endif
}
